<?php

use yii\db\Migration;

/**
 * Class m190118_073807_other_init
 */
class m190118_073807_other_init extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('user_certification',[
            'id'=>$this->primaryKey(),
            'uid'=>$this->integer()->notNull()->comment("用户"),
            'type'=>$this->tinyInteger()->notNull()->comment("类型"),
            'status'=>$this->tinyInteger()->notNull()->defaultValue(0)->comment("状态"),
            'real_name'=>$this->string()->comment("真实姓名"),
            'id_card'=>$this->string()->comment("身份证号"),
            'phone'=>$this->string()->comment("电话号码"),
            'token'=>$this->string(),
            'img'=>$this->integer()->comment("认证图片"),
            'notify'=>$this->string()->comment("认证回执"),
            'created_at'=>$this->integer()->comment("创建时间"),
            'updated_at'=>$this->integer()->comment("最后修改时间"),
        ]);
        $this->addForeignKey('fk_certification_user_uid','user_certification','uid','user','id','NO ACTION');
        $this->addForeignKey('fk_certification_image_img','user_certification','img','image','id','NO ACTION');


        $this->createTable('job_record',[
            'id'=>$this->primaryKey(),
            'uid'=>$this->integer()->notNull()->comment("用户"),
            'jid'=>$this->integer()->notNull()->comment("工作"),
            'description'=>$this->string()->comment("备注"),
            'status'=>$this->tinyInteger()->comment("状态"),
        ]);
        $this->addForeignKey('fk_record_user_uid','job_record','uid','user','id');
        $this->addForeignKey('fk_record_job_jid','job_record','jid','job','id');


        $this->createTable('order',[
            'id'=>$this->primaryKey(),
            'uid'=>$this->integer()->comment("用户"),
            'status'=>$this->tinyInteger()->comment("状态"),
            'type'=>$this->tinyInteger()->comment('类型'),
            'good_id'=>$this->integer()->comment("商品"),
            'number'=>$this->tinyInteger()->comment("数量"),
            'total_price'=>$this->integer()->unsigned()->comment("总价"),
            'location'=>$this->string()->comment("收货地址"),
            'notify'=>$this->string()->comment("备注"),
            'created_at'=>$this->integer()->comment("创建时间"),
            'updated_at'=>$this->integer()->comment("最后更新时间"),
        ]);
        $this->addForeignKey('fk_order_user_uid','order','uid','user','id');

        $this->createTable('user_consumption',[
            'id'=>$this->primaryKey(),
            'uid'=>$this->integer()->comment("用户"),
            'order_id'=>$this->integer()->comment("订单"),
            'money'=>$this->integer()->unsigned()->comment("价格"),
            //1：收入 2：消费
            'type'=>$this->tinyInteger()->notNull()->defaultValue('1')->comment("流动类型"),
            'created_at'=>$this->integer()->comment("时间"),
        ]);
        $this->addCommentOnTable('user_consumption',"用户消费记录表");
        $this->addForeignKey('fk_consumption_user_uid','user_consumption','uid','user','id');
        $this->addForeignKey('fk_consumption_order_orderid','user_consumption','order_id','order','id');

    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
//        echo "m190118_073807_other_init cannot be reverted.\n";
        $this->dropTable('user_certification');
        $this->dropTable('job_record');
        $this->dropTable('order');
        $this->dropTable('user_consumption');
        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m190118_073807_other_init cannot be reverted.\n";

        return false;
    }
    */
}
